import java.util.Stack;

public class Test {
    //逆波兰表达式
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i < tokens.length;i++){
            String s = tokens[i];
            if(!isOperation(s)){
                //数字字符
                stack.push(Integer.valueOf(s));
            }else{
                //+ - * /之一
                //n1比n2先入栈，因此后出
                int n2 = stack.pop();
                int n1 = stack.pop();
                switch(s){
                    case "+":
                        stack.push(n1+n2);
                        break;
                    case "-":
                        stack.push(n1-n2);
                        break;
                    case "*":
                        stack.push(n1*n2);
                        break;
                    case "/":
                        stack.push(n1/n2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperation(String s){
        if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
            return true;
        }
        return false;
    }
    //判断是否为栈的弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        //1、遍历pushV数组每次拿到一个数据就放到栈中
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0;i < pushV.length;i++){
            stack.push(pushV[i]);
            while(!stack.empty() && j < popV.length && stack.peek() == popV[j]){
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

    //括号匹配
    /*只要遇到左括号就入栈
    遇到右括号就开始匹配
    1、和栈顶不匹配
    2、栈为空
    3、栈不为空 但是字符串已经遍历完了
    * */
    public boolean isValid(String s) {
        //分析：为什么要用栈？
        //栈的插入和删除时间复杂度是O（1），对于这个题更合适
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i < s.length();i++){
            char ch = s.charAt(i);
            //进来的一定是括号
            if(ch == '(' ||ch == '['||ch =='{'){
                stack.push(ch);
            }else{
                //进来的先是右括号
                if(stack.empty()){
                    return false;
                }
                //此时左括号是栈顶元素，右括号是ch
                char top = stack.peek();
                if(top == '(' && ch == ')'
                        || top == '[' && ch == ']'
                        || top == '{' && ch =='}'){
                    stack.pop();
                }else{//左括号和右括号不匹配
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
        return true;
    }
    public static void main(String[] args) {
    MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        //弹出栈顶元素
        System.out.println(myStack.peek());//3
        System.out.println(myStack.pop());//3
        System.out.println(myStack.pop());//2
        System.out.println(myStack.pop());//1
        System.out.println(myStack.isEmpty());
        System.out.println(myStack.pop());//exception
    }
    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        System.out.println(stack.pop());//删除    出栈
        System.out.println(stack.peek());//获取栈顶元素但不删除
        System.out.println(stack.size());//获取栈的长度
        System.out.println(stack.empty());//判断栈是否为空
    }
}